const app = getApp();
Page({
  data: {
    userInfo: {},
    start: 1,
    length: 10000, // 目前是1w家店铺的设计 在更改距离计算后优化
    isloadmore: 1,
    list: [],
    isnodata: 0,
    fromid: '',
    attachurl: app.siteInfo.attachment_url,
    isLocation: false,
  },
  onLoad: function(options) {
    this.setData({
      opts: options || {},
      fromid: options.pid || '',
      themData: app.setThemeColor() || false,
    });
    this.loadList();
  },

  // 获取该商户下的所有店铺 分页
  loadList: function() {
    let that = this;
    let start = that.data.start;
    let length = that.data.length;
    wx.getLocation({
      success: function(res) {
        app.util.request({
          url: 'entry/wxapp/api',
          data: {
            type: 2,
            start: start,
            length: length,
            pid: that.data.fromid,
            lng: res.longitude,
            lat: res.latitude,
          },
          module: 'cm_micro_single_page',
          cachetime: '0',
          success(res) {
            console.log(res);
            if (res.data.data.length < length) {
              that.setData({
                isloadmore: 0,
              });
            } else {
              that.setData({
                isloadmore: 1,
              });
            }

            let arr = that.data.isLocation ? that.data.list : [];
            let newarr = arr.concat(res.data.data);
            console.log(newarr);
            let isnodata = (newarr.length === 0) ? 1 : 0;
            that.setData({
              list: newarr,
              start: start + 1,
              isnodata: isnodata,
              isLocation: true,
            });
          },
        });
      },
      fail: function(res) {
        console.log('用户点击了取消');
        app.util.request({
          url: 'entry/wxapp/api',
          data: {
            type: 2,
            start: start,
            length: length,
            pid: that.data.fromid,
            lng: '',
            lat: '',
          },
          module: 'cm_micro_single_page',
          cachetime: '0',
          success(res) {
            if (res.data.data.length < length) {
              that.setData({
                isloadmore: 0,
              });
            } else {
              that.setData({
                isloadmore: 1,
              });
            }

            let arr = that.data.list;
            let newarr = arr.concat(res.data.data);

            let isnodata = (newarr.length === 0) ? 1 : 0;
            that.setData({
              list: newarr,
              start: 1,
              isnodata: isnodata,
            });
          },
          fail(res) {
            wx.showToast({
              title: '获取公司列表信息失败！',
            });
          },
        });
      },
    });
  },
  // 查看地图
  goMap: function(e) {
    let that = this;
    if (that.data.isLocation) {
      let value = e.currentTarget.dataset;
      let lat = Number(value.lat);
      let lng = Number(value.lng);
      let title = value.title;
      let address = value.address;
      let QQMapWX = require('../../../common/utils/qqmap/qqmap-wx-jssdk.min.js');
      new QQMapWX({
        key: '3ESBZ-VPYWF-7Q7JL-NXY3M-4AVQT-T4BLE',
      }).reverseGeocoder({
        location: {
          latitude: lat,
          longitude: lng,
        },
        coord_type: 3, // baidu经纬度
        success: function(res) {
          let location = res.result.ad_info.location;
          wx.openLocation({
            latitude: location.lat,
            longitude: location.lng,
            name: title,
            address: address,
            scale: 28,
          });
        },
        fail() {
          wx.showToast({
            title: '位置信息获取失败！',
          });
        },
      });
    } else {
      if (e.detail.authSetting['scope.userLocation']) {
        wx.showToast({
          title: '授权成功',
          icon: 'success',
          duration: 1000,
        });
        // 再次授权，调用getLocationt的API
        that.loadList();
      } else {
        wx.showToast({
          title: '授权失败',
          icon: 'success',
          duration: 1000,
        });
      }
    }

  },
  // 分店详情
  goIndex: function(e) {
    const { id } = e.currentTarget.dataset;
    const page = getCurrentPages();
    const prepage = page[page.length - 2];
    prepage.setData({
      'param.id': id,
    }, () => {
      prepage.loadDefault();
      wx.navigateBack();
    });
  },
});